﻿/// Copyright (c) 2012 John Tear
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
/// and associated documentation files (the "Software"), to deal in the Software without restriction, 
/// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
/// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
/// subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial 
/// portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
/// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
/// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
/// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
/// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
///

using System;
using System.Collections.Generic;
using System.Text;

using com.feelgoodsoftware.sharptftp.utilities;

namespace com.feelgoodsoftware.sharptftp
{
    public class TftpReadWritePacket : TftpBasePacket
    {
        /// <summary>
        /// The filename in the packet.
        /// </summary>
        public string Filename { get; set; }

        /// <summary>
        /// The binary mode of the file in the data packet.
        /// </summary>
        public string Mode { get; set; }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="Bytes">The bytes received on the network wire.</param>
        public TftpReadWritePacket(byte[] Bytes)
        {
            // record the correct op code
            OpCode = (TftpOpCode)Bytes[1];

            // find the first zero octet and then record the filename
            // store an index of the first zero octet for later
            int zeroOctet = -1;

            for (int i = 2; i < Bytes.Length; i++)
            {
                if (Bytes[i] == 0)
                {
                    Filename = Encoding.ASCII.GetString(Bytes, 2, i - 2);
                    zeroOctet = i;
                    break;
                }
            }

            // if there is no filename then there is a problem in the packet
            if (string.IsNullOrEmpty(Filename))
            {
                // todo: we have a problem - throw a tftp exception
                throw new NotImplementedException();
            }

            // find the second zero octet and then record the mode
            for (int i = zeroOctet + 1; i < Bytes.Length; i++)
            {
                if (Bytes[i] == 0)
                {
                    Mode = Encoding.ASCII.GetString(Bytes, zeroOctet + 1, i - 3 - Filename.Length);
                    // make it lower case
                    Mode = Mode.ToLower();
                    break;
                }
            }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="OpCode">The opcode of the packet.</param>
        /// <param name="Filename">The filename to request/write.</param>
        /// <param name="Mode">The binary mode to use.</param>
        public TftpReadWritePacket(TftpOpCode OpCode, string Filename, string Mode)
        {
            this.OpCode = OpCode;
            this.Filename = Filename;
            this.Mode = Mode.ToLower();
        }

        public override byte[] ToByteArray()
        {
            byte[] returnArray = new byte[2] { 0, (byte)OpCode };
            returnArray = ByteArray.Combine(returnArray, Encoding.ASCII.GetBytes(Filename));
            returnArray = ByteArray.Combine(returnArray, new byte[1] { 0 });
            returnArray = ByteArray.Combine(returnArray, Encoding.ASCII.GetBytes(Mode));
            returnArray = ByteArray.Combine(returnArray, new byte[1] { 0 });

            return returnArray;
        }
    }
}
